// import store from "../../../store";
import { assign } from 'min-dash'
import store from '@/store'

export default function ContextPadProvider (
  config,
  injector,
  eventBus,
  contextPad,
  modeling,
  elementFactory,
  connect,
  create,
  popupMenu,
  canvas,
  rules,
  translate
) {
  this.modeling = modeling
  this.elementFactory = elementFactory
  this.connect = connect
  this.create = create
  this.translate = translate
  config = config || {}

  if (config.autoPlace !== false) {
    this.autoPlace = injector.get('autoPlace', false)
  }

  contextPad.registerProvider(this)
}

ContextPadProvider.$inject = [
  'config.contextPad',
  'injector',
  'eventBus',
  'contextPad',
  'modeling',
  'elementFactory',
  'connect',
  'create',
  'popupMenu',
  'canvas',
  'rules',
  'translate'
]

ContextPadProvider.prototype.getContextPadEntries = function (element) {
  if (!store.getters.processCanEdit) {
    return {}
  }

  const {
    autoPlace,
    create,
    elementFactory,
    translate,
    modeling,
    connect
  } = this

  function appendAction (type, className, title, options) {
    function appendStart (event, element) {
      var shape = elementFactory.createShape(assign({ type: type }, options))
      create.start(event, shape, {
        source: element
      })
    }

    var append = autoPlace
      ? function (event, element) {
        var shape = elementFactory.createShape(
          assign({ type: type }, { ...options, x: 600 })
        )
        shape.x = 1000
        autoPlace.append(element, shape)
      }
      : appendStart

    return {
      group: 'model',
      className: className,
      title: title,
      action: {
        dragstart: appendStart,
        click: append
      }
    }
  }

  function startConnect (event, element) {
    connect.start(event, element)
  }

  function removeElement (e) {
    modeling.removeElements([element])
  }

  // function clickElement (e) {
  //   // store.commit("SETNODEINFO", element);
  //   // store.commit("TOGGLENODEVISIBLE", true);
  //   console.log('属性自定义')
  // }

  /**
   * 开始节点没有操作
   */
  if (element.type === 'bpmn:StartEvent') {
    return []
  }

  /**
   * 开始节点的流程线
   */
  if (element.type === 'bpmn:SequenceFlow' && element.businessObject.sourceRef.$type === 'bpmn:StartEvent') {
    return []
  }

  var actions = {}
  if (
    element.type === 'bpmn:StartEvent' ||
    element.type === 'bpmn:UserTask' ||
    element.type === 'bpmn:ServiceTask' ||
    element.type === 'bpmn:ScriptTask' ||
    element.type === 'bpmn:ExclusiveGateway' ||
    element.type === 'bpmn:ParallelGateway' ||
    element.type === 'bpmn:IntermediateCatchEvent' ||
    element.type === 'bpmn:IntermediateThrowEvent' ||
    element.type === 'bpmn:ReceiveTask' ||
    element.type === 'bpmn:InclusiveGateway'
  ) {
    actions = {
      'append.user-task': appendAction(
        'bpmn:UserTask',
        'el-icon-user',
        '用户任务'
      ),
      'append.service-task': appendAction(
        'bpmn:ServiceTask',
        'el-icon-setting',
        '服务任务'
      ),
      'append.service-script-task': appendAction(
        'bpmn:ServiceTask',
        'el-icon-document-copy',
        '脚本任务',
        {
          _dataAction: 'create.service-script-task'
        }
      ),
      // 'append.script-task': appendAction(
      //   'bpmn:ScriptTask',
      //   'bpmn-icon-script-task',
      //   '脚本任务'
      // ),
      // 'append.send-task': appendAction(
      //   'bpmn:SendTask',
      //   'bpmn-icon-send-task',
      //   '发送任务'
      // ),
      'append.exclusive-gateway': appendAction(
        'bpmn:ExclusiveGateway',
        'el-icon-circle-close',
        '排他网关'
      ),
      'append.parallel-gateway': appendAction(
        'bpmn:ParallelGateway',
        'el-icon-circle-plus-outline',
        '并行网关'
      ),
      /*
      'append.timer-intermediate-event': appendAction(
        'bpmn:IntermediateCatchEvent',
        'bpmn-icon-intermediate-event-catch-timer',
        '定时任务',
        { eventDefinitionType: 'bpmn:TimerEventDefinition' }
      ), */
      'append.end-event': appendAction(
        'bpmn:EndEvent',
        'el-icon-video-pause',
        '结束节点'
      ),
      connect: {
        group: 'edit',
        className: 'el-icon-top-right',
        title: '连接工具',
        action: {
          click: startConnect,
          dragstart: startConnect
        }
      }
    }
  }

  /*
  if (
    element.type === 'bpmn:UserTask' ||
    element.type === 'bpmn:ServiceTask' ||
    element.type === 'bpmn:ScriptTask' ||
    element.type === 'bpmn:SequenceFlow'
  ) {
    assign(actions, {
      edit: {
        group: 'edit',
        className: 'bpmn-icon-business-rule',
        title: translate('属性'),
        action: {
          click: clickElement
        }
      }
    })
  }

   */

  if (element.type === 'bpmn:Lane' || element.type === 'bpmn:Participant') {
    actions = {
      'lane-insert-above': {
        group: 'lane-insert-above',
        className: 'bpmn-icon-lane-insert-above',
        title: translate('Add Lane above'),
        action: {
          click: function (event, element) {
            modeling.addLane(element, 'top')
          }
        }
      },
      'lane-insert-below': {
        group: 'lane-insert-below',
        className: 'bpmn-icon-lane-insert-below',
        title: translate('Add Lane below'),
        action: {
          click: function (event, element) {
            modeling.addLane(element, 'bottom')
          }
        }
      }
    }
  }

  // console.log('customcontxtpadprovider', element)

  // 是否初始化的节点
  // isInit true
  // let initValue = 'false'
  // const values = element.businessObject.extensionElements?.values
  // if (element.businessObject.$type === 'bpmn:UserTask' && values) {
  //   const field = values.find(item => item.name === 'init')
  //   initValue = field?.string?.body
  // }

  if (element.id !== '__initiator__') {
    assign(actions, {
      delete: {
        group: 'edit',
        className: 'el-icon-delete',
        title: '移除',
        action: {
          click: removeElement
        }
      }
    })
  }

  // console.log(actions)
  return actions
}
